package com.sanhe.a448.adapter;


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.menu.app.db.bean.CusUsage;
import com.menu.app.db.bean.Custom;
import com.menu.app.util.Utils;
import com.sanhe.a448.bean.WorkParam;
import com.sanhe.a448.databinding.ParamListItemBinding;
import com.sanhe.a448.frag.ParamDlg;

import java.util.ArrayList;
import java.util.List;

public class ParamAdapter extends RecyclerView.Adapter {

    List<CusUsage> list;
    Custom custom;
    ParamDlg dlg;
    final List<Long> delete = new ArrayList<>();
    boolean showDelete = false;

    public ParamAdapter(ParamDlg dlg, List list, Custom custom) {
        this.list = list;
        this.custom = custom;
        this.dlg = dlg;
    }

    public boolean cancelDelete() {
        if (showDelete) {
            showDelete = false;
            notifyItemRangeChanged(0, getItemCount());
            return true;
        }
        return false;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ParamListItemBinding binding = ParamListItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ItemHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemHolder) {
            ((ItemHolder) holder).bind(list.get(position), position);
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class ItemHolder extends RecyclerView.ViewHolder {

        ParamListItemBinding binding;

        public ItemHolder(ParamListItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public final void bind(CusUsage data, int position) {
            Gson g = Utils.getGson();
            WorkParam param = g.fromJson(g.toJson(data.info), WorkParam.class);
            binding.handle.setText(param.getHandle());
            binding.mode.setText(param.getMode());
            binding.time.setText(param.time + "");
            binding.energy.setText(param.energy + "");

            binding.index.setText(String.valueOf(position + 1));

            if (showDelete) {
                binding.index.setCompoundDrawablesWithIntrinsicBounds(com.menu.app.R.mipmap.delete_record_item, 0, 0, 0);
            } else {
                binding.index.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
            }
            binding.index.setOnClickListener(v -> {
                if (showDelete) {
                    delete.add(data.id);
                    list.remove(data);
                    int p = getBindingAdapterPosition();
                    notifyItemRemoved(p);
                    notifyItemRangeChanged(p, getItemCount() - p);
                } else {
                    dlg.toWork(data);
                }
            });
            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (!showDelete) {
                        showDelete = true;
                        notifyItemRangeChanged(0, getItemCount());
                    }
                    return true;
                }
            });
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (cancelDelete()) {
                        return;
                    }
                    dlg.toWork(data);
                }
            });
        }
    }

    public final List getDelete() {
        return delete;
    }
}
